package com.net.wanjian.networkhospitalmanager.view;

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

/**
 * Description:
 * Created by lidongcai on 2017/8/3.
 * Email:  lidongcaiocc@163.com
 */

public class RecyclerScrollView extends NestedScrollView {
    private int downX;
    private int downY;
    private int mTouchSlop;

    public RecyclerScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public RecyclerScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public RecyclerScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) e.getRawX();
                downY = (int) e.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) e.getRawY();
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(e);
    }


    //以下为监听scrollview滑动底部
    private int flag=0;    //并发控制标志位

    private OnZdyScrollViewListener onZdyScrollViewListener;

    //listview加载完毕，将并发控制符置为0
    public void loadingComponent(){
        flag=0;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        View view=this.getChildAt(0);
        //如果scrollview滑动到底部并且并发控制符为0，回调接口向服务器端请求数据
        if (this.getHeight() + this.getScrollY() == view.getHeight() && flag == 0) {
            flag = 1;//一进来就将并发控制符置为1，虽然onScrollChanged执行多次，但是由于并发控制符的值为1，不满足条件就不会执行到这
            onZdyScrollViewListener.ZdyScrollViewListener();
        }
    }

    public void setOnZdyScrollViewListener(OnZdyScrollViewListener onZdyScrollViewListener){
        this.onZdyScrollViewListener=onZdyScrollViewListener;
    }

    public interface OnZdyScrollViewListener{
        public void ZdyScrollViewListener();
    }


}
